# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-gromox
pkgver=2.34
pkgrel=0
pkgdesc="Open Source Groupware Solution"
arch="all !riscv64 !s390x" # test failure on s390x
url="https://grommunio.com/"
license="AGPL-3.0-or-later"
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev $pkgname-openrc"
_php=php83
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-install $pkgname.post-upgrade"
depends="
	grommunio-common
	$_php
	$_php-fpm
	postfix
	valkey
	!kopano-core
"

makedepends="
	autoconf
	automake
	curl-dev
	gumbo-parser-dev
	jsoncpp-dev
	krb5-dev
	ldns-dev
	libhx-dev
	libtool
	libvmime-dev
	libxml2-dev
	linux-pam-dev
	linux-headers
	mariadb-dev
	musl-dev
	openldap-dev
	openssl-dev
	$_php-dev
	sqlite-dev
	tinyxml2-dev
	xxhash-dev
	zlib-dev
	zstd-dev
"

pkgusers="grommunio gromox"
pkggroups="grommunio gromox gromoxcf"

source="
	https://github.com/grommunio/gromox/archive/refs/tags/gromox-$pkgver.tar.gz
	0001-iconv-ignore.patch

	gromox-delivery-queue.initd
	gromox-delivery.initd
	gromox-event.initd
	gromox-http.initd
	gromox-imap.initd
	gromox-midb.initd
	gromox-pop3.initd
	gromox-timer.initd
	gromox-zcore.initd
	delivery.cfg
	event.cfg
	http.cfg
	imap.cfg
	midb.cfg
	mysql_adaptor.cfg
	pop3.cfg
	smtp.cfg
	timer.cfg
	zcore.cfg
	autodiscover.ini
	grommunio-virtual-mailbox-domains.cf
	grommunio-virtual-mailbox-alias-maps.cf
	grommunio-virtual-mailbox-maps.cf
	grommunio-bcc-forwards.cf
"

builddir="$srcdir/gromox-gromox-$pkgver/"

prepare() {
	default_prepare
	autoreconf -fiv
}

build() {
	export CXXFLAGS="$CXXFLAGS -fpermissive"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var
	make
}

check() {
	make check
}

package() {
	mkdir -p "$pkgdir"
	make install DESTDIR="$(realpath $pkgdir)"

	# prepare services
	for item in delivery-queue delivery event http imap midb pop3 timer zcore; do
		install -Dm 0755 "$srcdir"/gromox-$item.initd "$pkgdir"/etc/init.d/gromox-$item
	done

	# prepare configuration. some files are predefined, others are generated.
	for item in delivery event http imap midb mysql_adaptor pop3 smtp timer zcore; do
		install -Dm 0640 "$srcdir"/$item.cfg "$pkgdir"/etc/gromox/$item.cfg
	done

	# autodiscover.ini
	install -Dm 0640 "$srcdir"/autodiscover.ini "$pkgdir"/etc/gromox/autodiscover.ini

	# mod_cache configuration
	echo "* /web /usr/share/webapps/grommunio-web" > "$pkgdir"/etc/gromox/cache.txt

	# mod_fastcgi configuration
	for i in dav sync web; do
		echo "* /$i /usr/share/webapps/grommunio-$i php index.php | /run/php-fpm${_php##php}/php-grommunio-$i-fpm.sock" \
			>> "$pkgdir"/etc/gromox/fastcgi.txt
	done

	# postfix files
	install -Dm 0640 -g postfix "$srcdir"/grommunio-bcc-forwards.cf "$pkgdir"/etc/postfix/grommunio-bcc-forwards.cf
	install -Dm 0640 -g postfix "$srcdir"/grommunio-virtual-mailbox-alias-maps.cf "$pkgdir"/etc/postfix/grommunio-virtual-mailbox-alias-maps.cf
	install -Dm 0640 -g postfix "$srcdir"/grommunio-virtual-mailbox-domains.cf "$pkgdir"/etc/postfix/grommunio-virtual-mailbox-domains.cf
	install -Dm 0640 -g postfix "$srcdir"/grommunio-virtual-mailbox-maps.cf "$pkgdir"/etc/postfix/grommunio-virtual-mailbox-maps.cf

	# license
	install -Dm 0644 LICENSE.txt "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# create database directories
	for i in /var/lib/gromox \
		/var/lib/gromox/domain \
		/var/lib/gromox/user \
		/var/lib/gromox/queue \
		/var/lib/gromox/queue/cache \
		/var/lib/gromox/queue/mess \
		/var/lib/gromox/queue/save \
		/var/lib/gromox/queue/timer; do
		install -dm 0770 -g gromox -o gromox "$pkgdir"/$i
	done

	# make sure all files in /etc/gromox are owned by grommunio:grmoxcf
	chown -R grommunio:gromoxcf "$pkgdir"/etc/gromox
	find  "$pkgdir"/etc/gromox -type f -exec chmod 0640 {} ';'
	find  "$pkgdir"/etc/gromox -type d -exec chmod 0750 {} ';'

	# remove unnecessary folders
	rm -r "$pkgdir"/usr/lib/systemd "$pkgdir"/usr/lib/sysusers.d/ "$pkgdir"/usr/lib/tmpfiles.d/

}

sha512sums="
1e45a4b76b565d1120aebd3fe19cdf67d5a5e7328c4bb3053e2e728631204fce6f3bdf0133c36ad4492ba85bf89a4b9db00b8d636636469d169a6877b28d898a  gromox-2.34.tar.gz
c26bf6a32e1537d428e61908dc07613e37c17dbb858461045d98813396711083e94c5138469c4a66304006369586a00a156a49fb27660900963e656cff9486ad  0001-iconv-ignore.patch
88bd2ef0f586e5147554d960d8c448b4896de05ca3d397a94131d409409a2bad1a858b67cfac41f609125f60471f53f03df53b44b04fd28ef6dede50a0e5d7fe  gromox-delivery-queue.initd
7081e5e45ecea883a5bf6ebba17ab3bd70543f15745456a4f33cb853078f66fdb8d20a950281a4cc6b8b6f32c4c5123f82bbeda5be7fa3226ac32fb61e715c42  gromox-delivery.initd
b21657db42dcd4186a5e848871743933dbff0649726620fe487f827fca47c4f1dd874d34667ebbf3c0d3e30fc16ff4915bede9a74fd582f0e435fefa1779255c  gromox-event.initd
b73835c9b28c5db8b1af490600c5a67376656d36ea2ba9cd454bd56338ecaa78de12cd63bbfd6b63c3dbbba20cdd3f3717c93c8b24cce0d7e0e977c9c61e7dbb  gromox-http.initd
03aadb6f56911b793cbe531d945efe396f9c1a683d39dfaf8044309ac07c2020ea4e4eb00c864c7f1121137e140cafe8f8ef2b820d8dcd8c89b6c1c4093ac14e  gromox-imap.initd
33a29162ed9bca961bdee4c63d172055aabcf7b85003db9e467861b2b9e476796cf6e11d952fe05d6428a719674030806c3f1364c912f3569c552f0e0e73e186  gromox-midb.initd
92b300a2465e9abb91af7cffc95c6dca45fe4f03ddcc7ac8d31eef281dbe10410565227483ecf8357aecfe8be5c74b6ce8b83ec2741b2c3fb25455c06dc6dd2c  gromox-pop3.initd
b0fcb6d7add8bed4a1faa192a7672d2b5cd7c0e118bb91d954081610fd00399c20469d2f6434485f8804c14af3e5fd9814027b66e1cdba2de370f8187a7d2635  gromox-timer.initd
d375d63d1bc13207aabb906d8455d167795e88c0400ac420e4ec1103eed70062b04a0b5640852041257161fc11609eca8ad9097742d50973546e3cbfac47a7c4  gromox-zcore.initd
e610fd0cb1392f08cd5cd30450fc9d6cdd6a2724ff439a38a2c6623755c9e2afdeee2f9d45d965ba0dd043b5c3942c1a0d69061f7cc9b1ecdd680109e7d756de  delivery.cfg
b9f4d736c41278b320bae89e5c0d6674fd5b628bc6fb6c5b88b15e37617f39d228a884988aeaba5b2fbe05a64372ed7f8094cdc363a2a1901b0c6c1735844e92  event.cfg
281a6deb683722222a34e149003e6d361ca41f45ca73d32a46a37a757cf39847e5e90868f9af454f9ee59fcf570716c011ee5087a02e705f68b0ded73c2c4c43  http.cfg
ff5f2bb87d49c8127eefd1c26e2480a9c8e9730d10e890a69b9ba2bf9b4e8c11b5841b4c8be59a6fbeb0eb3c803fb3323a1cc971c1c571ed11719e169f4745bd  imap.cfg
07997f87175bc835059d7a9c8b59fc282bb6e835c9ea34b87e7f2ec90a27d522a44f19d28b4ab9bcd009053f88756b6ebe9de7edbbde2ded24bd56c5767251db  midb.cfg
a4b54f32a4f8b932ea93dd71c1eaccac50a445d960f3ab37913481db3b80daad51238f4b826331111e939f5a75d522619fff9566042670b578c65c477754bd64  mysql_adaptor.cfg
43db0b8d9d65d5e1d86195875cc564d11a0c4d5970a571bc97dc6150d74d8f9c456adb0ac1db9cc84302923e285e2f6d9b564dfd92a8039e97e5f98834327b4a  pop3.cfg
a0cf62b2982479faa3e00ec401ae1946505eb7c581507f45757663e5e9b922a126c96e56e1a64090016420f4e045c83efffe06e725c96f9887c745b6581bbfb2  smtp.cfg
f910bae21e35f79552ae79899263ca8d1ba7bfd85944cc0915e4dda83e35600cdce00b17346d788ea3c0060fea4cd1525d5781524ce086712d51b49c6bc0cd44  timer.cfg
f1ecee83fa72137bba15e11f109c8501bbc8712c8a5fa41aab3468da05e1ef6a61c5ac03488688222a7cd4e54fa9c2e6696819471d6d8deb9193865033327f8d  zcore.cfg
91b433070e89dfc4b2d7ea9360547bc2d1dd37bd220c3e688994249d52f17d980c15f41ec52505aa027644633546a61742da3f3155c267d99bc0c37956f118f3  autodiscover.ini
8890e170ef2c9b1cc43ab84c2c24446f20be69dd395ec8249246568aaadcd440a304103a36d008da8cbf20ec8ec0522dadf2217ee4ee00255c58a442d94ec263  grommunio-virtual-mailbox-domains.cf
2ce6b5867a9dd9ef5091e1b167813a14900958be9fe5ba46bf333a58bd7b9fe3d8dfd2a25d65a1ae567dc362ec98ea648d4ae5a95f87370a083d6707c1bda475  grommunio-virtual-mailbox-alias-maps.cf
22161519cacb4f97577ca17216b9d2aa0212c32d7d7d6f3d82c8894628f4b6ab7471cf49530bebe22f1a96747f3907ec684ce0d9c9980a2621cc65581913dec0  grommunio-virtual-mailbox-maps.cf
929f5a44d7a343c11e3a10e5d972e4ec2362f08126efa45fa61f1b70efd01a650df2aaff7140b39d7b88b4754cabc0fac3880dfdc2e91232b6f129fe4ad2326d  grommunio-bcc-forwards.cf
"
